﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Media.Animation;

namespace Microsoft.WVB.Silverlight.Infrastructure
{
    public class LayoutManager : ILayoutManager
    {
        private System.Collections.Generic.List<IManagedControl> minimisedControls;
        private Dictionary<IManagedControl, IManagedControl> _managedControls = null;
        private Canvas mainCanvas;
        private IManagedControl maximsedControl;

        public LayoutManager()
        {
            _managedControls = new Dictionary<IManagedControl,IManagedControl>();
            this.minimisedControls = new System.Collections.Generic.List<IManagedControl>();
        }
                
        public StackPanel TopButtonPanel { get; set; }

        public void RegisterMainCanvas(Canvas canvas)
        {
            this.mainCanvas = canvas;
            this.mainCanvas.SizeChanged += new SizeChangedEventHandler(mainCanvas_SizeChanged);
        }

        void mainCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.maximsedControl != null)
            {
                Canvas.SetLeft((FrameworkElement)this.maximsedControl, 15);
                Canvas.SetTop((FrameworkElement)this.maximsedControl, 15);

                ((FrameworkElement)this.maximsedControl).Width = this.mainCanvas.ActualWidth - 30;
                ((FrameworkElement)this.maximsedControl).Height = this.mainCanvas.ActualHeight - 30;
            }
        }

        public void ShowControl(IManagedControl control, Rect rectangle)
        {
            if (!_managedControls.ContainsKey(control))
            {
                _managedControls.Add(control,control);
                control.Activated += new EventHandler(control_Activated);
            }
            this.mainCanvas.Dispatcher.BeginInvoke(delegate
            {
                control.Control.SetValue(Canvas.LeftProperty, rectangle.X);
                control.Control.SetValue(Canvas.TopProperty, rectangle.Y);
                RestoreControl(control);
            });
        }
        public void ShowControl(IManagedControl control)
        {
            if (!_managedControls.ContainsKey(control))
            {
                _managedControls.Add(control, control);
                control.Activated += new EventHandler(control_Activated);
            }
            this.mainCanvas.Dispatcher.BeginInvoke(delegate
            {
                RestoreControl(control);
            });
        }

        public void RegisterControl(IManagedControl control)
        {
            if (!_managedControls.ContainsKey(control))
            {
                _managedControls.Add(control, control);
                control.Activated += new EventHandler(control_Activated);
            }
        }
        void control_Activated(object sender, EventArgs e)
        {
            if (sender is IManagedControl)
                RestoreControl(sender as IManagedControl);
        }
        void topbutton_Click(object sender, RoutedEventArgs e)
        {
            RestoreControl((sender as Button).Tag as IManagedControl);
        }

        public void CloseAllWindows()
        {
            foreach (var item in _managedControls.Keys)
            {
                item.Control.Visibility = Visibility.Collapsed;
            }
        }

        public void MinimizeAllWindows()
        {
            foreach (var item in _managedControls.Keys)
            {
                if (item.Control.Visibility == Visibility.Visible)
                {
                    item.Control.Visibility = Visibility.Collapsed;
                    this.minimisedControls.Add(item);
                }
            }
        }

        public void RestoreAllWindows()
        {
            ////// Iterate through the previously minimised controls and restore them
            ////foreach (IManagedControl item in this.minimisedControls)
            ////{
            ////    this.RestoreControl(item);
            ////}

            ////this.minimisedControls.Clear();

            //foreach (var item in _managedControls.Keys)
            //{
            //    this.RestoreControl(item);
            //}
        }

        public void RestoreControl(IManagedControl control)
        {
            foreach (var item in _managedControls.Keys)
            {
                Canvas.SetZIndex(item.Control, Canvas.GetZIndex(item.Control) - 1);
                if (item.LayoutGroup == control.LayoutGroup && control.LayoutMode == LayoutGroupMode.Exclusive)
                    item.Control.Visibility = Visibility.Collapsed;
            }
            this._managedControls[control].Control.Visibility = Visibility.Visible;
            Canvas.SetZIndex(this._managedControls[control].Control, 0x3e8);

            if (control == this.maximsedControl)
            {
                this.maximsedControl = null;
            }
            
            if (control.NormalSize != Size.Empty)
            {
                if (Double.IsNaN(control.NormalSize.Height) == false)
                {
                    ((FrameworkElement)control).Height = control.NormalSize.Height;
                }
                else
                {
                    ((FrameworkElement)control).Height = Double.NaN;
                }


                if (Double.IsNaN(control.NormalSize.Width) == false)
                {
                    ((FrameworkElement)control).Width = control.NormalSize.Width;
                }
                else
                {
                    ((FrameworkElement)control).Width = Double.NaN;
                }
            }
            else
            {
                ContentControl windowContent = control as ContentControl;

                windowContent.Width = Double.NaN;
                windowContent.Height = Double.NaN;
                windowContent.InvalidateArrange();
                //windowContent.UpdateLayout();
                //((FrameworkElement)windowContent.Content).InvalidateMeasure();
                //((FrameworkElement)windowContent.Content).UpdateLayout();
                
            }
        }

        public void MaximiseControl(IManagedControl controlToMaximise)
        {
            foreach (var item in _managedControls.Keys)
            {
                if (item != controlToMaximise)
                {
                    Canvas.SetZIndex(item.Control, Canvas.GetZIndex(item.Control) - 1);

                    // TODO -  Check if setting the ZIndex of the other controls is enough

                    ////if (item.LayoutGroup == control.LayoutGroup && control.LayoutMode == LayoutGroupMode.Exclusive)
                    ////    item.Control.Visibility = Visibility.Collapsed;
                }
            }

            controlToMaximise.Control.Visibility = Visibility.Visible;
            Canvas.SetZIndex(controlToMaximise.Control, 0x3e8);

            controlToMaximise.StashNormalSize();

            this.maximsedControl = controlToMaximise;
        }

        
    }
}
